package com.zmn.oms.admin.common;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 类描述: 百度AK
 *
 * @author liuying
 * @date 2022/06/04 23:41
 */
@Slf4j
@Component
public class BaiduAkUtil {
    private static long webAkCount = 0;
    private static long addOrderWebAkCount = 0;

    // 主AK Lh3Dt8AlQaStSKDldLdG0a5q
    private static String PRIMARY_WEB_AKS = "";
    // 录单专用 1659aM4Ikyrl06XRAuwIVYSuBzjarHGr
    private static List<String> ADD_ORDER_WEB_AKS = Lists.newArrayList();
    // 其它使用
    private static List<String> OTHERS_WEB_AKS = Lists.newArrayList();

    // 录单专用 1659aM4Ikyrl06XRAuwIVYSuBzjarHGr
    @NacosValue(value = "${baidu.ak.web.addOrder:''}", autoRefreshed = true)
    public void setAddOrderWebAks(List<String> aks){
        ADD_ORDER_WEB_AKS = aks;
        log.info("设置录单专用AK");
    }

    // 主AK Lh3Dt8AlQaStSKDldLdG0a5q
    @NacosValue(value = "${baidu.ak.web.primary:''}", autoRefreshed = true)
    public void setPrimaryWebAks(String ak){
        PRIMARY_WEB_AKS = ak;
        log.info("设置主授权AK");
    }

    // 其它使用
    @NacosValue(value = "${baidu.ak.web.others:}", autoRefreshed = true)
    public void setOthersWebAks(List<String> aks){
        OTHERS_WEB_AKS = aks;
        log.info("设置其它AK");
    }

//    "o3s1Xsxh7RGhKIGDCD0ruQ7pUTAWHBXD",
//    "G1Zl6aksZvhig9rv57trqQpPEtRWFYyx",
//    "42M0ERTjXNIsXYEdVjKIEXQnvgBvWAtP",
//    "ONiYp5fblpKbXiOuuSyRvWNIAkeiGe7x",
//    "7tQVF9go5MayoaUrRhdTGvv2s16hBHRM",
//    "dlHU41UgpHE4iALnVpwawatx2W2m3u3s",
//    "jz9sqbRWEyaVpe37V1yCpii0BM3RKXb7",
//    "SrTH2vkwUIDaG9OWYEXC5IdX2PcxzM9V"

    /**
     * 获取百度 ak， 仅限页面上使用
     *
     * @return
     */
    public static String getWebAk() {
        if (StringUtils.isNotBlank(PRIMARY_WEB_AKS)) {
            return PRIMARY_WEB_AKS;
        }

        webAkCount++;
        if (webAkCount > 100000) {
            webAkCount = 1;
        }
        int size = OTHERS_WEB_AKS.size();
        int index = (int) webAkCount % size;
        String webAk = OTHERS_WEB_AKS.get(index);
        return webAk;
    }



    /**
     * 获取百度 ak， 仅限页面上使用
     *
     * @return
     */
    public static String getAddOrderWebAk() {
        addOrderWebAkCount++;
        if (addOrderWebAkCount > 100000) {
            addOrderWebAkCount = 1;
        }
        int size = ADD_ORDER_WEB_AKS.size();
        int index = (int) addOrderWebAkCount % size;
        String webAk = ADD_ORDER_WEB_AKS.get(index);
        return webAk;
    }

    public static void main(String[] args) {
        System.out.println(BaiduAkUtil.getAddOrderWebAk());
        System.out.println(BaiduAkUtil.getAddOrderWebAk());
        System.out.println(BaiduAkUtil.getAddOrderWebAk());
        System.out.println(BaiduAkUtil.getAddOrderWebAk());
    }
}
